<?php
class Response{

	public function getLatestResponse($id) {
		$sql_get_response_id = "SELECT MAX(`id`)FROM `response` WHERE `student_id` = '$id'";
		$rid = mysql_query($sql_get_response_id)or die(mysql_error());
		$result_get_response_id = mysql_fetch_row($rid);
		foreach($result_get_response_id as $key=>$value) {
			$response_id = $value;
		}
		if($response_id) {
			return $response_id;
		} else {
			return false;
		}
	}
	public function saveResponse($data) {
		$sql = "INSERT INTO `response` (`survey_id`,`student_id`,`student_name`,`timestamp`)
	       VALUES ( '{$data['survey_id']}','{$data['student_id']}','{$data['student_name']}','{$data['timestamp']}')";
		$result = mysql_query($sql)or die ("Save Response: ". mysql_error());
		if($result) {
			return true;
		} else {
			return false;
		}
	}
	public function saveResponseValues($data) {
		$sql = "INSERT INTO `response_value` (`response_id`,`survey_id`,`question_id`,`response`)
	       VALUES ('{$data['response_id']}','{$data['survey_id']}','{$data['question_id']}','{$data['response']}')";
		$result = mysql_query($sql)or die("Save Response Value: ".mysql_error());
		if($result) {
			return true;
		} else {
			return false;
		}

	}
	public function updateResponseValues($data){
		$sql = "UPDATE `response_value` SET `response`='{$data['response']}'
			WHERE `question_id`='{$data['question_id']}' AND `response_id`='{$data['response_id']}'";
		$result = mysql_query($sql)or die("Save Response Value: ".mysql_error());
		if($result) {
			return true;
		} else {
			return false;
		}
	}
	public function checkResponse($response_id, $question_id) {
		$sql = "SELECT * FROM `response_value` WHERE `question_id`='$question_id' AND `response_id`='$response_id'";
		$result = mysql_query($sql)or die("Check Question Response: ".mysql_error());
		$response = mysql_fetch_row($result);
		if($response) {
			return true;
		} else {
			return false;
		}
	}
	public function getResponseValue($response_id,$question_id) {
		$sql = "SELECT `response` FROM `response_value` WHERE `question_id`='$question_id' AND `response_id`='$response_id'";
		$result = mysql_query($sql)or die("GET Question Response: ".mysql_error());
		$response = mysql_fetch_object($result);
		if($response) {
			return $response->response;
		} else {
			return false;
		}
	}
}
?>